package com.cloud.logger.zchain.zchainserver.interceptor;

import com.alibaba.fastjson.JSON;
import com.cloud.logger.zchain.core.common.constant.ResultCodeConstant;
import com.cloud.logger.zchain.core.exception.BusinessException;
import com.cloud.logger.zchain.zchainserver.base.GlobalConstants;
import com.cloud.logger.zchain.zchainserver.base.ResultMessage;
import com.cloud.logger.zchain.zchainserver.util.GlobalMap;
import com.cloud.logger.zchain.zchainserver.util.LoginUserThreadLocal;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 浙江卓锐科技股份有限公司
 *
 * @author zhengyt
 * @create 2017/12/19
 * @description
 */
public class LoginInterceptor implements HandlerInterceptor {

    private static String TOKEN_CANNOT_EMPTY = "token不能为空！";

    private static String TOKEN_INVALID = "token失效";

    private static String CONTENT_TYPE = "Content-type";

    private static String CHARSET = "text/html;charset=UTF-8";

    private static String LOGIN_EXCEPTION = "登录异常！";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {

        if("OPTIONS".equals(request.getMethod()))
            return true;

            //获取会话里面登陆的用户token
            Cookie[] cookies = request.getCookies();
            if (cookies == null || cookies.length == 0) {
                throw new BusinessException((int) ResultCodeConstant.TOKEN_INVALID_ERROR_CODE,TOKEN_CANNOT_EMPTY);
            }
            for (Cookie cookie : cookies) {
                if (GlobalConstants.COOKIE_NAME.equals(cookie.getName())) {

                    if (GlobalMap.getUserMap(cookie.getValue()) == null) {
                        throw new BusinessException((int) ResultCodeConstant.TOKEN_INVALID_ERROR_CODE,TOKEN_INVALID);
                    }

                    LoginUserThreadLocal.setCurrentUser(cookie.getValue());
                    return true;
                }
            }
            throw new BusinessException((int) ResultCodeConstant.TOKEN_INVALID_ERROR_CODE,TOKEN_CANNOT_EMPTY);

    }

    public OutputStream getOutputStream(HttpServletResponse response,String msg,boolean isSuccess,long errCode){
        try {
            response.setHeader(CONTENT_TYPE, CHARSET);
            OutputStream outputStream = response.getOutputStream();
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.setMessage(msg);
            resultMessage.setCode(errCode);
            resultMessage.setIsSuccess(isSuccess);
            outputStream.write(JSON.toJSONString(resultMessage).getBytes("utf-8"));
            return outputStream;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}